package com.erp.admin.business.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.base.base.BaseController;
import com.erp.base.base.Page;
import com.erp.base.business.entity.Provincial;
import com.erp.base.business.service.ProvincialService;
import com.erp.base.common.utils.R;
/**
 * 省份表
 * 
 * @author wanghw
 * @email whongwei_yz@sina.com
 * @date 2018-10-22 11:31:33
 */
 
@Controller
@RequestMapping("/business/provincial")
public class ProvincialController extends BaseController{
	@Autowired
	private ProvincialService provincialService;
	
	@GetMapping()
	@RequiresPermissions("business:provincial:provincial")
	String Provincial(){
	    return "business/provincial/provincial";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("business:Provincial:provincial")
	public Page<Provincial> list(Provincial provincial,HttpServletRequest request,HttpServletResponse response){
		//查询列表数据
		Page<Provincial> page= Page.bootstrapPage(request, response);
		page = provincialService.findPage(page, provincial);
		return page;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("business:provincial:add")
	String add(){
	    return "business/provincial/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("business:provincial:edit")
	String edit(@PathVariable("id") Long id,Model model){
		Provincial provincial = provincialService.get(id);
		model.addAttribute("provincial", provincial);
	    return "business/provincial/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("business:provincial:add")
	public R save( Provincial provincial,Model model){
		if(!beanValidator(model, provincial)){
			return R.error((String)model.asMap().get("message"));
		}
		try {
				provincialService.save(provincial);
				return R.ok();
			} catch (Exception e) {
				logger.error("保存失败", e);
				return R.error();
			}
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("business:provincial:edit")
	public R update( Provincial provincial,Model model){
		if(!beanValidator(model, provincial)){
			return R.error((String)model.asMap().get("message"));
		}
		try {
				provincialService.save(provincial);
				return R.ok();
			} catch (Exception e) {
				logger.error("保存失败", e);
				return R.error();
			}
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("business:provincial:remove")
	public R remove( Long id){
		try {
			provincialService.delete(id);
			return R.ok();
		} catch (Exception e) {
			logger.error("删除失败， id:"+ id, e);
			return R.error();
		}
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("business:provincial:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		try {
			provincialService.deleteBatch(Arrays.asList(ids));
			return R.ok();
		} catch (Exception e) {
			logger.error("删除失败，ids:"+ids, e);
			return R.error();
		}

	}
	
}
